package com.example.myui.activity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.myui.R;
import com.example.myui.activity.service.BindService;
import com.example.myui.activity.service.MyIntentService1;

public class BindServiceActivity extends AppCompatActivity {

    private static final String TAG = BindServiceActivity.class.getSimpleName();
    private LocalBroadcastManager instance;
    public static final String ACTION_TYPE_SERVICE = "action_type_service";
    public static final String ACTION_TYPE_THREAD = "action_type_thread";
    private TextView result;
    private Receiver receiver;
    public BindService.MyBinder binder;
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (BindService.MyBinder) service;
            binderService = binder.getService();


            result.append("duration" + binderService.getDuration() + "\n");

            result.append("progress" + binderService.getProgress() + "\n");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            binder = null;
            binderService = null;
        }
    };
    private BindService binderService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_service);

        initBroadcast();
        result = findViewById(R.id.tv_result);
        findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText("");
            }
        });
        findViewById(R.id.btn_start_intentservice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BindServiceActivity.this, MyIntentService1.class);
                intent.putExtra("str", "hahahah");
                startService(intent);
            }
        });

        findViewById(R.id.btn_stop_intentservice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BindServiceActivity.this, MyIntentService1.class);
                stopService(intent);
            }
        });

        findViewById(R.id.btn_bind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BindServiceActivity.this, BindService.class);
                intent.putExtra("bind","bind");
                bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
            }
        });

        findViewById(R.id.btn_unbind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(mConnection);
            }
        });
    }

    private void initBroadcast() {
        instance = LocalBroadcastManager.getInstance(this);
        receiver = new Receiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_TYPE_SERVICE);
        intentFilter.addAction(ACTION_TYPE_THREAD);
        instance.registerReceiver(receiver, intentFilter);
    }

    private class Receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case ACTION_TYPE_SERVICE:
                    String service = intent.getStringExtra("service");
                    result.append(service + "\n");
                    break;
                case ACTION_TYPE_THREAD:
                    String thread = intent.getStringExtra("thread");
                    result.append(thread + "\n");
                    break;
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance.unregisterReceiver(receiver);
    }
}
